// PlayerController.ts
import { _decorator, Component, Vec2, Animation } from 'cc';
import { Joystick } from '../Joystick';
import { CharacterState } from '../enum';
import { ICharacterState } from './states/CharacterState';
import { StandState } from './states/StandState';
import { RunStates } from './states/RunStates';
const { ccclass, property } = _decorator;

@ccclass('PlayerController')
export class PlayerController extends Component {
    @property
    moveSpeed: number = 200;

    @property(Joystick)
    public joystick: Joystick = null;

    // public get joystick(): Joystick {
    //     return this._joystick;
    // }

    private currentState: ICharacterState;
    private stateMap: Map<CharacterState, ICharacterState> = new Map();

    public playerAnim: Animation = null;

    start() {
        this.playerAnim = this.node.getComponent(Animation);
        
        // 初始化状态机
        this.stateMap.set(CharacterState.STAND, new StandState());
        this.stateMap.set(CharacterState.RUN, new RunStates());
        this.transitionTo(CharacterState.STAND);
    }

    public transitionTo(newState: CharacterState) {
        if (this.currentState) {
            this.currentState.exit(this);
        }
        this.currentState = this.stateMap.get(newState);
        this.currentState.enter(this);
    }

    update(deltaTime: number) {
        // const direction = this.joystick.getDirection();
        
        // // 保留移动计算但移除位置更新 ← 这是问题根源
        // const moveDelta = new Vec2(
        //     direction.x * this.moveSpeed * deltaTime,
        //     direction.y * this.moveSpeed * deltaTime
        // );
        
        this.currentState.update(this, deltaTime);
    }
}